'use strict';
// 申请开通控制器模块
import {
    viewInit,
    djsfunc
} from '../views/changePhoneView.js';
var cw = require("../components/winLocation.js");
import {
    errorDialog
} from '../components/errorDialog.js';
import {
    Logout
} from '../components/logout.js';
var logoutObject;

var token, loginName, openid, loginType;
var $verifyBtn, $phone, $verification_code, $newPhone;
var errorObject;
$(document).on("pageinit", "#changePhone_page", controllerInit);

// 控制器初始化
function controllerInit() {
    cw.checkBrowser();
    initValue();
    viewInit();
    bindEvent();
}

// 发送修改手机号
function submitChange() {
    if ($("#submit_btn").hasClass("weui-btn_disabled")) {
        return;
    } else {
        var scParameters = getscParameters();
        $.post(ModifyPhone_URL, scParameters, cpback);
    }
}

// 修改手机号回调函数
function cpback(cpdata) {
    if (cpdata.login_response) {
        haslogin(cpdata);
        return;
    }
    console.dir(cpdata);
    if (cpdata.lfq_merextrainfo_response && cpdata.lfq_merextrainfo_response.resp_code == "0000") {
        errorObject.showErroDialog("修改手机号成功,3秒后返回到设置页面");
        setTimeout(function() {
            location.href = "./userSettings.html";
        }, 3000);
    } else if (cpdata.lfq_merextrainfo_response && cpdata.lfq_merextrainfo_response.resp_code != "0000" && cpdata.lfq_merextrainfo_response.resp_msg) {
        errorObject.showErroDialog(cpdata.lfq_merextrainfo_response.resp_msg);
    } else if (cpdata.resp_msg) {
        errorObject.showErroDialog(cpdata.resp_msg);
    } else {
        errorObject.showErroDialog("修改手机号失败");
    }
}

// 修改手机号的参数
function getscParameters() {
    var opval = $phone.val(),
        npval = $newPhone.val(),
        vfcode = $verification_code.val();

    var scParameters = {
        "oldphone": opval,
        "contactPhone": npval,
        "verificationCode": vfcode,
        "token": token,
        "loginName": loginName,
        "bizLicNo": loginName,
        "openid": openid,
        "loginType": loginType
    };

    return scParameters;
}

// 获取验证码
function getVerifyCode() {
    if ($verifyBtn.hasClass("weui-btn_plain-disabled")) {
        return;
    } else {
        djsfunc();
        $verifyBtn.attr("class","weui-btn_plain-disabled");
        var parameters = getsmsParameters();
        $.post(duanxin_URL, parameters, smsCallBack);
    }
}
//短信发送 回调函数
function smsCallBack(msg) {
    if (msg.login_response) {
        haslogin(msg);
        return;
    }
    console.dir(msg);
    if (msg.code != "00") {
        errorObject.showErroDialog(msg.msg);
    }

}

// 取发送短信参数
function getsmsParameters() {
    var parameters = {};
    parameters.lodinId = $("#phone").val();
    parameters.position = loginType;
    parameters.sendType = "3"; //发送类型
    //获取url中的openid
    parameters.openid = openid;
    return parameters;
}

// 判断是否有效登入
function haslogin(msg) {
    if (msg.login_response) {
        errorObject.showErroDialog("该账户已在别的地方登录,请重新登入");
        setTimeout(function() {
            location.href = LoginInit_URL;
        }, 3000);
    }
}


// 绑定事件
function bindEvent() {
    $verifyBtn.on("touchstart", getVerifyCode)
    $("#submit_btn").on("touchstart", submitChange)
}

// 初始化变量
function initValue() {
    $verifyBtn = $("#verifyBtn");
    $phone = $("#phone");
    $verification_code = $("#verification_code");
    $newPhone = $("#new-phone");
    errorObject = new errorDialog();
    getSessionStorage();
    logoutObject = new Logout();
}

// 取本地存储essionStorage
function getSessionStorage() {
    token = sessionStorage.getItem("tltd_token");
    loginName = sessionStorage.getItem("tltd_loginName");
    openid = sessionStorage.getItem("tltd_openid");
    loginType = sessionStorage.getItem("tltd_loginType");
    console.log(token, loginName, openid, loginType);
}